package edu.xlh.travel.dao;

import edu.xlh.travel.pojo.Hotel;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

public interface HotelDao extends JpaRepository<Hotel,String>, JpaSpecificationExecutor<Hotel> {

    @Modifying
    @Query(value = "update t_hotel set " +
            "name = CASE WHEN :#{#hotel.name} IS NULL THEN name ELSE :#{#hotel.name} END," +
            "price = CASE WHEN :#{#hotel.price} IS NULL THEN price ELSE :#{#hotel.price}  END," +
            "star= CASE WHEN  :#{#hotel.star} IS NULL THEN star ELSE :#{#hotel.star} END, " +
            "bed_num= CASE WHEN  :#{#hotel.bedNum} IS NULL THEN bed_num ELSE :#{#hotel.bedNum} END, " +
            "addr= CASE WHEN  :#{#hotel.addr} IS NULL THEN addr ELSE :#{#hotel.addr} END, " +
            "stock= CASE WHEN  :#{#hotel.stock} IS NULL THEN stock ELSE :#{#hotel.stock} END, " +
            "miaoshu= CASE WHEN  :#{#hotel.miaoshu} IS NULL THEN miaoshu ELSE :#{#hotel.miaoshu} END " +
            "where id =:#{#hotel.id}",nativeQuery = true )
    void updateHotelByHotelId(@Param("hotel")Hotel hotel);

    @Modifying
    @Query(value = "update t_hotel set " +
            "miaoshu= CASE WHEN  :#{#hotel.miaoshu} IS NULL THEN miaoshu ELSE :#{#hotel.miaoshu} END " +
            "where id =:#{#hotel.id}",nativeQuery = true )
    void editMiaoShu(@Param("hotel")Hotel hotel);

    @Transactional
    @Modifying
    @Query(value = "update Hotel set delFlag =?2 where id =?1")
    void updateScenicDelFlagByScenicId(String hotelId, String delFlag);
}
